package controllers;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import models.Picture;
import models.Video;

import org.apache.commons.io.FileUtils;
import play.Play;
import play.mvc.Controller;
import play.mvc.With;
import service.PictureService;
import service.VideoService;
import util.ResourceOperate;

@With(Secure.class)
public class VideoAction extends Controller{
	public static String[] types = {"asx","flv","avi","wmv","mp4","mov","asf","mpg","rm","rmvb"};
	/**
	 * 全查
	 */
	@Check("ResourceManage")
	public static void list(){
		List<Video> vidList = VideoService.findAll();
		render(vidList);
	}
	
	@Check("ResourceManage")
	public static void blank(){
		render();
	}
	
	/**
	 * 添加上传
	 * @param name
	 * @param file
	 * @param dis
	 */
	@Check("ResourceManage")
	public static void createvid(File file, String vidDis) {
		try{
			boolean b = (Boolean) ResourceOperate.upload(file,types).get(0);
			if(b){
				String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
				Video vid = new Video();
				vid.vidName = ResourceOperate.splitStr(file.getName()).get(0).toString(); // 拿到文件名
				vid.vidPath = uploadPath;
				vid.vidDis = vidDis;
				VideoService.create(vid);
				flash.success("上传成功 ！");
				list();
			}else{
				flash.success("上传失败 ！");
				list();
			}
		} catch (Exception e) {
			blank();
			flash.success("上传失败 ！");
			e.printStackTrace();
			list();
		}
	}
	
	/**
	 *修改信息
	 * @param id
	 */
	@Check("ResourceManage")
	public static void show(long id){
		Video vid  = VideoService.findById(id);
		render(vid);
	}
	/**
	 * 修改
	 * @param name
	 * @param path
	 * @param dis
	 * @param file
	 * @param id
	 */
	@Check("ResourceManage")
	public static void saveVid(String vidName, String vidPath, String vidDis,
			File file, long id) {
		try {

			Video vid = VideoService.findById(id);
			String oldName = vid.vidName;
			vid.vidName = vidName;
			vid.vidDis = vidDis;
			if (file != null) {
				boolean bb = (Boolean) ResourceOperate.upload(file,types).get(0);
				if(bb){
					boolean b = ResourceOperate.deleteFile(Play.applicationPath + vidPath);
					if (b) {
						String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
						String ends =  ResourceOperate.splitStr(uploadPath).get(1).toString();
						vid.vidPath = uploadPath.replace(file.getName().toString(), vidName+"."+ends); // 原名
						ResourceOperate.reName(uploadPath, vidName); 		// 修改文件夹中的文件名称
					}
					VideoService.save(vid); 	// 将修改后的数据保存到数据库中
					flash.success("修改成功 ！");
					list();
				}else{
					flash.success("修改失败 ！");
					list();
				}	
			} else {
				String path = vidPath.replace(oldName, vidName); // 将目录下的文件名称修改
				vid.vidPath = path;
				ResourceOperate.reName(vidPath, vidName);
				VideoService.save(vid); 	// 将修改后的数据保存到数据库中
				flash.success("修改成功 ！");
				list();
			}
				
		} catch (Exception e) {
			flash.success("修改失败 ！");
			list();
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@Check("ResourceManage")
	public static void deleteVid(long id){
		String vidPath = Play.applicationPath+VideoService.findById(id).vidPath;
		boolean b =ResourceOperate.deleteFile(vidPath);
		if(b){
			VideoService.delete(id);
			flash.success("删除成功 ！");
		}else{
			flash.success("删除失败 ！");
		}
		
		list();
	}
	
	
}

